﻿define(function (require) {

    var DateView = require('view/DateView');
    var UpdateView = require('view/UpdateView');
    var NotificationView = require('view/NotificationView');

    return Backbone.View.extend({
        el: $(".ui-listview"),
        events: {
            "click .feed-close": "clear"
        },

        initialize: function () {
            _.bindAll(this, 'addOne', 'addAll', 'render');
            this.model.bind('add', this.render);
            this.model.bind('remove', this.render);
        },
        date: null,
        render: function () {
            $(".ui-listview").empty();
            this.date = null;
            this.addAll();
        },
        addEl: function (view) {
            var element = view.render().el;
            this.el.append(element);
        },
        addOne: function (e) {
            var currentDay = new Date(
                    Date.fromEpoch(e.get("UpdateOn")).getFullYear(),
                    Date.fromEpoch(e.get("UpdateOn")).getMonth(),
                    Date.fromEpoch(e.get("UpdateOn")).getDate()
                ).toEpoch();

            if (e.get('Type') == "notification") {
                var view = new NotificationView({ model: e });
                this.addEl(view);
            } else if (e.get('Type') == "update") {

                if (this.date != currentDay) {
                    var view = new DateView({ model: e });
                    this.addEl(view);
                    this.date = currentDay;
                }

                var view = new UpdateView({ model: e });
                this.addEl(view);
            }
        },

        addAll: function () {
            this.model.each(this.addOne);
        },
        clear: function (e) {
            var cid = $(e.target).data('cid');
            var feedModel = this.model.getByCid(cid);
            this.model.remove(feedModel);
        }

    });

});